package com.performanceManagement.service.impl;

import com.performanceManagement.dao.EmployeeInformationDao;
import com.performanceManagement.dao.PerformanceManagementDao;
import com.performanceManagement.pojo.EmployeeInformation;
import com.performanceManagement.pojo.PerformanceManagement;
import com.performanceManagement.service.EmployeeInformationService;
import com.performanceManagement.service.PerformanceManagementService;
import com.performanceManagement.util.ExcelUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class PerformanceManagementServiceImpl implements PerformanceManagementService {

    //注入dao
    @Autowired
    PerformanceManagementDao performanceManagementDao;
    @Autowired
    EmployeeInformationDao employeeInformationDao;
    //注入employeeInformationService
    @Autowired EmployeeInformationService employeeInformationService;
    /**
     * 生成一个PerformanceManagement对象，以实现代码的复用
     * @param isAskForLeave
     * @param isLate
     * @param isAbsent
     * @param isLeaveEarly
     * @param workAttitude
     * @param workCompleteness
     * @return PerformanceManagement
     */

    @Override
   public PerformanceManagement setPerformanceManagement (String isAskForLeave, String isLate, String isAbsent, String isLeaveEarly, String workAttitude, String workCompleteness,Integer eId) {
       PerformanceManagement performanceManagement = new PerformanceManagement();
       performanceManagement.setIsAskForLeave(isAskForLeave);
       performanceManagement.setIsLate(isLate);
       performanceManagement.setIsAbsent(isAbsent);
       performanceManagement.setIsLeaveEarly(isLeaveEarly);
       performanceManagement.setWorkAttitude(workAttitude);
       performanceManagement.setWorkCompleteness(workCompleteness);
       performanceManagement.setpDate(new Date());
       EmployeeInformation employeeInformation = new EmployeeInformation();
       employeeInformation.seteId(eId);
       performanceManagement.setEmployeeInformation(employeeInformation);
       return performanceManagement;
   }

    /**
     *
     * @param eId
     */
    @Override
    public void add(Integer eId) {
        PerformanceManagement performanceManagement = new PerformanceManagement();
        performanceManagement.setpDate(new Date());
        EmployeeInformation employeeInformation = new EmployeeInformation();
        employeeInformation.seteId(eId);
        performanceManagement.setEmployeeInformation(employeeInformation);
        performanceManagementDao.save(performanceManagement);
    }

    /**
     * 系统自生成日期的添加操作
     * @param isAskForLeave
     * @param isLate
     * @param isAbsent
     * @param isLeaveEarly
     * @param workAttitude
     * @param workCompleteness
     */
   @Override
    public void add(String isAskForLeave, String isLate, String isAbsent, String isLeaveEarly, String workAttitude, String workCompleteness,Integer eId) {
       PerformanceManagement performanceManagement = setPerformanceManagement(isAskForLeave,isLate,isAbsent,isLeaveEarly,workAttitude,workCompleteness,eId);
       performanceManagementDao.save(performanceManagement);
    }


    /**
     * 自定义日期的添加操作
     * @param isAskForLeave
     * @param isLate
     * @param isAbsent
     * @param isLeaveEarly
     * @param workAttitude
     * @param workCompleteness
     * @param date
     */
    @Override
    public void add(String isAskForLeave, String isLate, String isAbsent, String isLeaveEarly, String workAttitude, String workCompleteness, Date date,Integer eId) {
        PerformanceManagement performanceManagement =setPerformanceManagement(isAskForLeave,isLate,isAbsent,isLeaveEarly,workAttitude,workCompleteness,eId);
        performanceManagement.setpDate(date);
        performanceManagementDao.save(performanceManagement);
    }


    /**
     * 通过部门名称查询员工绩效
     * @param eDepartment
     * @return
     */
    @Override
    public List<PerformanceManagement> queryByEDepartment(String eDepartment) {
        //查询处当前部门的所有员工
        List<EmployeeInformation> employeeInformationList = employeeInformationService.queryByEDepartment(eDepartment);

        //建立一个绩效ArrayList以存储每个员工的绩效表
        List<PerformanceManagement> performanceManagementAll = new ArrayList<>();

        //遍历每个员工的到其绩效表存到ArrayList中
        for (EmployeeInformation employeeInformation : employeeInformationList) {
            //通过员工信息得到其绩效表
            List<PerformanceManagement> performanceManagements = this.performanceManagementDao.findByEmployeeInformation(employeeInformation);
            //将所有的表加入到ArrayList中
            performanceManagementAll.addAll(performanceManagements);
        }
        return performanceManagementAll;
    }

    @Override
    public List<PerformanceManagement> queryByEName(String eName) {
        //通过EName得到该员工对象，EName可以重复，故用List集合存储
        List<EmployeeInformation> employeeInformationList = employeeInformationService.queryByEName(eName);
        //员工对象不唯一，故需要建立一个绩效ArrayList以存储每个员工的绩效表
        List<PerformanceManagement> performanceManagementAll = new ArrayList<>();
        //遍历每个员工的到其绩效表存到ArrayList中
        for (EmployeeInformation employeeInformation : employeeInformationList) {
            //通过员工信息得到其绩效表
            List<PerformanceManagement> performanceManagements = this.performanceManagementDao.findByEmployeeInformation(employeeInformation);
            //将所有的表加入到ArrayList中
            performanceManagementAll.addAll(performanceManagements);
        }
        return performanceManagementAll;
    }

    /**
     * poi的导出excel方法
     * @param fileName
     * @param performanceManagementList
     * @throws Exception
     */
    @Override
    public void exportExel(String fileName, List<PerformanceManagement>performanceManagementList) throws Exception{
        //没有元素，则不进行下载
        if (performanceManagementList.size()<=0)
            return;
        //新建一个工作簿
        Workbook workbook = new SXSSFWorkbook();
        //创建sheet;
        Sheet sheet = workbook.createSheet();
        Row rowT = sheet.createRow(0);
        Cell cellT1 = rowT.createCell(0);
        cellT1.setCellValue("员工编号");
        Cell cellT2 = rowT.createCell(1);
        cellT2.setCellValue("员工姓名");
        Cell cellT3 = rowT.createCell(2);
        cellT3.setCellValue("是否请假");
        Cell cellT4 = rowT.createCell(3);
        cellT4.setCellValue("是否迟到");
        Cell cellT5 = rowT.createCell(4);
        cellT5.setCellValue("是否旷工");
        Cell cellT6 = rowT.createCell(5);
        cellT6.setCellValue("是否早退");
        Cell cellT7 = rowT.createCell(6);
        cellT7.setCellValue("工作态度");
        Cell cellT8 = rowT.createCell(7);
        cellT8.setCellValue("工作完成度");
        Cell cellT9 = rowT.createCell(8);
        cellT9.setCellValue("绩效时间");

        for (int i = 0;i<performanceManagementList.size();i++){
            Row row = sheet.createRow(i+1);
            Cell cell1 = row.createCell(0);
            cell1.setCellValue(performanceManagementList.get(i).getEmployeeInformation().geteId());
            Cell cell2 = row.createCell(1);
            cell2.setCellValue(performanceManagementList.get(i).getEmployeeInformation().geteName());
            Cell cell3 = row.createCell(2);
            cell3.setCellValue(performanceManagementList.get(i).getIsAskForLeave());
            Cell cell4 = row.createCell(3);
            cell4.setCellValue(performanceManagementList.get(i).getIsLate());
            Cell cell5 = row.createCell(4);
            cell5.setCellValue(performanceManagementList.get(i).getIsAbsent());
            Cell cell6 = row.createCell(5);
            cell6.setCellValue(performanceManagementList.get(i).getIsLeaveEarly());
            Cell cell7 = row.createCell(6);
            cell7.setCellValue(performanceManagementList.get(i).getWorkAttitude());
            Cell cell8 = row.createCell(7);
            cell8.setCellValue(performanceManagementList.get(i).getWorkCompleteness());
            Cell cell9 = row.createCell(8);
            cell9.setCellValue(performanceManagementList.get(i).getpDate().toString());
        }
        fileName = fileName +".xlsx";
        FileOutputStream fout = new FileOutputStream(fileName);
        workbook.write(fout);
        fout.close();
        workbook.close();
    }


    /**
     * 下载到excl
     * @param title
     * @param sheetName
     * @param dataSet
     * @param filePath
     * @param fileName
     * @throws IOException
     */
    @Override
    public void excelExport(String title, String sheetName, Collection<PerformanceManagement> dataSet, String filePath, String fileName) throws IOException {
        ExcelUtils excelUtils = new ExcelUtils();
        excelUtils.excelExport(title,sheetName,PerformanceManagement.class,dataSet,filePath,fileName);
    }

    /**
     * 下载到excl
     * @param dataSet
     * @param filePath
     * @param fileName
     * @throws IOException
     */
    @Override
    public void excelExport(Collection<PerformanceManagement> dataSet,String filePath,String fileName) throws IOException {
        ExcelUtils excelUtils = new ExcelUtils();
        excelUtils.excelExport("员工绩效表","",PerformanceManagement.class, dataSet,filePath,fileName);
    }

    /**
     * 下载到excl
     * @param dataSet
     * @param fileName
     * @throws IOException
     */
    @Override
    public void excelExport(Collection<PerformanceManagement> dataSet, String fileName) throws IOException {
        ExcelUtils excelUtils = new ExcelUtils();
        excelUtils.excelExport("员工绩效表","",PerformanceManagement.class, dataSet,"",fileName);
    }


    /**
     * 下载到excl
     * @param dataSet
     * @throws IOException
     */
    @Override
    public void excelExport(Collection<PerformanceManagement> dataSet) throws IOException {
        ExcelUtils excelUtils = new ExcelUtils();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        String str = sdf.format(date);
        excelUtils.excelExport("员工绩效表","",PerformanceManagement.class, dataSet,"","员工绩效表"+str);
    }


}
